/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

import antlr.CommonAST;
import com.bayu.common.lib.CommonUtil;
import com.bayu.services.dao.StockDao;
import com.bayu.services.dao.TIncomingUsageDao;
import com.bayu.services.dao.TStockDao;
import com.bayu.services.entity.Stock;
import com.bayu.services.entity.TIncomingUsage;
import com.bayu.services.entity.TStock;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author submonkey
 */
@Controller
@RequestMapping("/stock-input.htm")
public class StockInput {
    TIncomingUsageDao dao;
    TStockDao stockDao;
    @Autowired
    private void setStockDao(TIncomingUsageDao dao){
      this.dao = dao;
    }

    @Autowired
    private void setTStockDao(TStockDao stockDao){
      this.stockDao = stockDao;
    }

     @RequestMapping(method = RequestMethod.GET)
    public void displayForm(ModelMap map,String id,String errormsg){
        map.addAttribute("errormsg", errormsg);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("tIncomingUsage") TIncomingUsage tIncomingUsage , BindingResult result,ModelMap map, String tanggal,HttpServletRequest req){
            DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tgl = null;
            String idStock = null;

            String[] tempIdStock = tanggal.split("-");

            idStock = tempIdStock[1]+tempIdStock[0];
            TStock tStock = null;

            List data = this.stockDao.getById(idStock);
            if(data.size()>0){
                tStock = (TStock) data.get(0);
            
           
                if(result.hasFieldErrors("tanggal")){
                    try {


                        tgl = formarter.parse(tanggal);

                        tIncomingUsage.setTanggal(tgl);
                        tIncomingUsage.setIdStock(tStock);
                    } catch (ParseException ex) {
                        Logger.getLogger(CastingInput.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                System.out.println("What idStock????"+idStock);
                List total = this.dao.custom("select count(*) from TIncomingUsage where tanggal='"+tanggal+"'");

                String strTotal=total.get(0).toString();


                int c = Integer.valueOf(strTotal).intValue();

                if(c > 0){
                    return "redirect:ErrorDuplicate.html";
                }else{
                    this.dao.save(tIncomingUsage);
                    return "redirect:view-stock-data.htm?msg=Data has been updated";
                }
            }else{
                String strErr = "Data initial stock bulan : "+ CommonUtil.getMonthName(Integer.parseInt(tempIdStock[1])-1)+" belum diisi!!";
                map.addAttribute("errormsg",strErr);
                return "redirect:stock-input.htm";
            }

    }
}
